//
//  SecondViewController.m
//  UI07_property_delegate_block
//
//  Created by Hanlingyu on 16/7/22.
//  Copyright © 2016年 ltq. All rights reserved.
//

#import "SecondViewController.h"
#import "FirstViewController.h"

@interface SecondViewController ()

@property (nonatomic, strong) UITextField *textField;
@end

@implementation SecondViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.textField = [[UITextField alloc] initWithFrame:CGRectMake(100, 100, 200, 50)];
    _textField.backgroundColor = [UIColor whiteColor];
//    _textField.text = self.titleString;
    [self.view addSubview:_textField];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.backgroundColor = [UIColor blueColor];
    button.frame = CGRectMake(100, 200, 50, 30);
    [button setTitle:@"上一页" forState:UIControlStateNormal];
    [self.view addSubview:button];
    [button addTarget:self action:@selector(comeBack:) forControlEvents:UIControlEventTouchUpInside];

    // 界面通信
    // 1. 属性传值 一般常用于从前往后传值
    // 想要传递的值, 座位最后一个页面的属性, 并通过该属性进行一系列操作
    // 将FirstViewController的titleLabel.text传递给SecondViewController的textFiled
    // FirstViewController -> 取到titleLabel.text
    //       ↓ push             ↓ 属性传值        
    // SecondViewController -> titleString -> 去使用titleString做任意事情
    
    // 从后往前传值(可以, 但是不推荐这样使用);
    // SecondViewController -> 取到 textField.text
    //   ↓ pop (获取First的VC)       ↓ 属性传值
    // FirstViewController -> 设置 titleTextField -> 去使用
    
    
    // 2. 协议传值(delegate)
    // 从后往前传值
    // Second -> First传值
    // 决定用代理方式传值
    // 要给Second声明协议
    // 1. 决定要在Second声明协议(最难确定)
    // 2. 声明delegate @property id <Delegate名> delegate
    // 3. 在哪签代理(只要可以拿到对象, 就可以签订代理)
    // XXX.delegate = self;
    // 4. XXX.delegate = self;会爆黄, 没签订协议, 签订协议
    // 5. 签订完协议, 会爆黄, 因为有必须实现的协议方法未实现, 实现协议方法
    // 6. 但是协议方法并没有执行, 缺少触发时机
    //    在哪触发该协议方法(加上对应的判断)
    
    
    
    
}

- (void)comeBack:(UIButton *)button {
    

    NSInteger count = self.navigationController.viewControllers.count;
    FirstViewController *firstVC = self.navigationController.viewControllers[count - 2];
    firstVC.titleTextFiled = _textField.text;
#warning 触发协议方法 (判断delegate是否存在, 判断delegate的类有没有实现协议方法)
    
//    if (self.delegate && [self.delegate respondsToSelector:@selector(passValue:)]) {
//        [self.delegate passValue:_textField.text];
//    }
//    
    [self.navigationController popViewControllerAnimated:YES];
    
    
}

- (void)dealloc
{
    NSLog(@"释放");
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
